using UnityEngine;
using System.Collections;

public class EditorCamera : MonoBehaviour {
	
	public float speed = 5;
	
	public float minX;
	public float maxX;
	public float minY;
	public float maxY;
	Transform myTransform;
	
	//float mDelta = 25; // Pixels. The width border at the edge in which the movement work
	//float mSpeed = 3.0f; // Scale. Speed of the movement
	
	// Use this for initialization
	void Start () {
		myTransform = transform;
	}
	
	public void SetViewCamera()
	{
		camera.orthographicSize = 5;
		Vector3 pos = transform.position;
		pos.x = -21;
		pos.y = -15;
		transform.position = pos;
	}
	
	public void SetMinMaxViewPos(GridSize grid)
	{
		this.minX = -15;
		this.minY = -3.9f;
		
		if(grid.X < 13)
			this.maxX = -15;
		else
			this.maxX = -15 + (grid.X - 13);
		
		if(grid.X < 9)
			this.maxY = -3.9f;
		else
			this.maxY = -3.9f + (grid.Y - 9) * 1.0f - 0.2f;
		
		//this.min = min;
		//this.max = max;
	}
	// Update is called once per frame
	void Update () 
	{
		/*
		if (Input.GetAxis("Mouse ScrollWheel") < 0) // back
        {
            Camera.main.orthographicSize = Mathf.Min(Camera.main.orthographicSize +1, 20);
        }
        else if (Input.GetAxis("Mouse ScrollWheel") > 0) // forward
        {
            Camera.main.orthographicSize = Mathf.Max(Camera.main.orthographicSize - 1, 5);
		}*/
		float moveX = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
		float moveY = Input.GetAxis("Vertical")   * Time.deltaTime * speed;
		//float newPos = moveX + myTransform.position.x;
		//if(min > newPos  && newPos < max)
		myTransform.Translate(moveX,/*Input.GetAxis("Vertical") * _speed*/moveY,0);
		
		Vector3 tmpPos = myTransform.position;
		tmpPos.x = Mathf.Clamp(tmpPos.x,minX,maxX);
		tmpPos.y = Mathf.Clamp(tmpPos.y,minY,maxY);
		
		myTransform.position = tmpPos;
		
	}
	/*
	void LateUpdate()
	{
   		// Check if on the right edge
	   if ( Input.mousePosition.x >= Screen.width - mDelta )
	   {
	      // Move the camera
	      transform.position += transform.right * Time.deltaTime * mSpeed;
	   }

	   if ( Input.mousePosition.x <= 0 + mDelta )
	   {
	      // Move the camera
	      transform.position -= transform.right * Time.deltaTime * mSpeed;
	   }

	   if ( Input.mousePosition.y >= Screen.height - mDelta )
	   {
	      // Move the camera
	      transform.position += transform.up * Time.deltaTime * mSpeed;
	   }
		
	   if ( Input.mousePosition.y <= 0 + mDelta )
	   {
	      // Move the camera
	      transform.position -= transform.up * Time.deltaTime * mSpeed;
	   }
	}*/
}
